<?php
ini_set('memory_limit', '50M');
ini_set('set_time_limit', 1800);
set_include_path($_SERVER['DOCUMENT_ROOT']);
$error = "网页状态已经过期，请刷新页面";
include_once 'admin/includes/functions/admin-functions.php';
global $DBobject, $SMARTY, $CONFIG;
//后台认证
if (empty($_SESSION['user']['admin'])) {
    header("location: http://www.readysteadygokids.com.cn/admin/login");
    die();
}
//数据查询
selectInfo();
//数据操作
if ($_POST['type'] == 'delete') {
    $sql = "UPDATE tbl_news_title SET deleted = NOW() WHERE id=:id";
    $params = [
        "id" => $_POST['id'],
    ];
    if ($res = $DBobject->wrappedSql($sql,$params)) {
        $news_title_id = $_POST['id'];
        $sql = "UPDATE tbl_news_management SET publish = '0', news_title_id = '-1' WHERE deleted IS NULL AND news_title_id = :news_title_id ";
        $DBobject->wrappedSql($sql, array(':news_title_id' => $_POST['id']));
        echo jsonHtml(1, '删除成功');
    } else {
        echo jsonHtml(0, '删除失败');
    }
} elseif($_POST['type'] == 'add') {
    if (checkToken('admin', $_POST['formToken'])) {
        $sql = 'INSERT INTO tbl_news_title (title, created, modified) VALUES ( :title, NOW(), NOW() ) ';
        $params = [
            "title" => $_POST['title'],
        ];
        if (selectRepeat($_POST['title']) == '1') {
            echo jsonHtml(0, '此标题已存在,请重新添加');
            die();
        }
        if ($_POST['title'] == '') {
            echo jsonHtml(0, '添加失败');
        }
        $res = $DBobject->wrappedSql($sql,$params);
        echo jsonHtml(1, '添加成功');
    } else {
        echo jsonHtml(0, '添加失败');
    }
} elseif($_POST['type'] == 'edit') {
    //表单提交
    if (checkToken('admin', $_POST['formToken'])) {
        $sql = "UPDATE tbl_news_title SET modified = NOW(), title = :title WHERE id=:id ";
        $params = [
            "id" => $_POST['id'],
            "title" => $_POST['title'],
        ];
        if (selectRepeat($_POST['title']) == '1') {
            echo jsonHtml(0, '此标题已存在,请重新编辑');
            die();
        }
        if ($_POST['title'] == '' || $_POST['id'] == '') {
            echo jsonHtml(0, '编辑失败');
        }
        $res = $DBobject->wrappedSql($sql,$params);
        echo jsonHtml(1, '编辑成功');
    } else {
        echo jsonHtml(0, '编辑失败');
    }
}

//json
function jsonHtml ($type , $content) {
     return json_encode(array(
        'type' => $type,
        'content' => $content
    ));
    die();
}

//主标题查询
function selectInfo () {
    global $DBobject, $SMARTY;
    $sql = "SELECT * FROM tbl_news_title WHERE deleted IS NULL ORDER BY modified DESC";
    $res = $DBobject->executeSQL($sql);
    $SMARTY->assign("res", unclean($res));
}
//主标题校验
function selectRepeat ($title) {
    global $DBobject;
    $sql = "SELECT * FROM tbl_news_title WHERE title = :title AND deleted IS NULL ";
    $param = [
        "title" => $title,
    ];
    $res = $DBobject->executeSQL($sql, $param);
    if (count($res) > 0) {
        return 1;
    } else {
        return -1;
    }
}
